package com.shellyan.realtime.gmallpublisher.service;

import com.shellyan.realtime.gmallpublisher.mapper.DauMapper;
import com.shellyan.realtime.gmallpublisher.mapper.OrderMapper;
import com.shellyan.realtime.gmallpublisher.util.ESUtil;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.TermsAggregation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Shelly An
 * @create 2020/9/4 15:49
 */

/**
 * 加了Service注解，可以将Autowired属性自动注入
 */
@Service
class PublisherServiceImp implements PublisherService{
    /**
     * 从mapper读数据，自动注入
     */
    @Autowired
    DauMapper dau;

    /**
     * 获取日活总数
     * @param date
     * @return
     */
    @Override
    public Long getDau(String date) {
        return dau.getDau(date);
    }

    /**
     * 每小时日活
     * @param date
     * @return
     */
    @Override
    public Map<String, Long> getHourDau(String date) {
        List<Map<String, Object>> hourDau = dau.getHourDau(date);
        HashMap<String, Long> result = new HashMap<>();

        for (Map<String, Object> map : hourDau) {
            String hour = map.get("LOGHOUR").toString();
            //实际生产中记得判断一下再强转，否则可能有风险
            Long count = (Long) map.get("COUNT");
            result.put(hour,count);
        }

        return result;
    }

    @Autowired
    OrderMapper order;

    @Override
    public BigDecimal getTotalAmount(String date) {
        return order.getTotalAmount(date);
    }

    @Override
    public Map<String, BigDecimal> getHourTotalAmount(String date) {
        List<Map<String, Object>> hourAmount = order.getHourTotalAmount(date);
        Map<String, BigDecimal> result = new HashMap<>();

        for (Map<String, Object> map : hourAmount) {
            String hour = map.get("CREATE_HOUR").toString();
            BigDecimal sum = (BigDecimal) map.get("SUM");
            result.put(hour,sum);
        }

        return result;
    }

    @Override
    public Map<String, Object> getSaleDetailAndAggs(String date, String keyword, int startPage, int size) throws IOException {
        JestClient client = ESUtil.getClient();
        Search.Builder builder = new Search.Builder(ESUtil.getDSL(date, keyword, startPage, size))
                .addIndex("gmall_sale_detail")
                .addType("_doc");
        SearchResult searchResult = client.execute(builder.build());
        client.close();

        HashMap<String, Object> result = new HashMap<>();
        Long total = searchResult.getTotal();
        result.put("total",total);

        List<SearchResult.Hit<HashMap, Void>> hits = searchResult.getHits(HashMap.class);
        ArrayList<HashMap> detail = new ArrayList<>();
        for (SearchResult.Hit<HashMap, Void> hit : hits) {
            HashMap source = hit.source;
            detail.add(source);
        }
        result.put("detail",detail);

        List<TermsAggregation.Entry> genderBuckets = searchResult
                .getAggregations()
                .getTermsAggregation("group_by_user_gender")
                .getBuckets();

        HashMap<String, Long> genderAgg = new HashMap<>();
        for (TermsAggregation.Entry bucket : genderBuckets) {
            String gender = bucket.getKey();
            Long count = bucket.getCount();
            genderAgg.put(gender,count);
        }

        result.put("genderAgg",genderAgg);



        List<TermsAggregation.Entry> ageBuckets = searchResult
                .getAggregations()
                .getTermsAggregation("group_by_user_age")
                .getBuckets();

        HashMap<String, Long> ageAgg = new HashMap<>();
        for (TermsAggregation.Entry bucket : ageBuckets) {
            String gender = bucket.getKey();
            Long count = bucket.getCount();
            ageAgg.put(gender,count);
        }

        result.put("ageAgg",ageAgg);

        return result;
    }
}

